package graduate.cslg.com.universityshopping.card;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import graduate.cslg.com.universityshopping.R;

public class CardActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView cardId;
    private TextView cardDate;
    private TextView cardBalance;
    private RelativeLayout dailyAccount;
    private RelativeLayout dailyDetail;
    private RelativeLayout cardLost;
    private RelativeLayout cardGet;
    private RelativeLayout passwordChange;
    private Toolbar mToolbar;
    private String userAccount;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);
        initViews();
        SharedPreferences preferences = getSharedPreferences("user_info",MODE_PRIVATE);
        userAccount = preferences.getString("user_account", "Z09315222");
        String userBalance = preferences.getString("card_balance", "0.00");
        cardBalance.setText("￥" + userBalance);
        cardId.setText(userAccount);
        int date = getDate(userAccount);
        cardDate.setText("(有效期至:" + date + "-06-30)");
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

    }

    private void initViews() {
        cardId = findViewById(R.id.card_stu_id);
        cardDate = findViewById(R.id.card_date);
        cardBalance = findViewById(R.id.card_balance);
        dailyAccount = findViewById(R.id.daily_account);
        dailyDetail = findViewById(R.id.daily_detail);
        cardLost = findViewById(R.id.daily_lost);
        cardGet = findViewById(R.id.daily_get);
        passwordChange = findViewById(R.id.password_alert);
        mToolbar = findViewById(R.id.toolbar);
        dailyAccount.setOnClickListener(this);
        dailyDetail.setOnClickListener(this);
        cardLost.setOnClickListener(this);
        cardGet.setOnClickListener(this);
        passwordChange.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.daily_account:
                startActivity(new Intent(this, DailyAccountActivity.class));
                break;
            case R.id.daily_detail:
                startActivity(new Intent(this, OrderDetailActivity.class));
                break;
            case R.id.daily_lost:
                startActivity(new Intent(this, LostFoundActivity.class));
                break;
            case R.id.daily_get:
                startActivity(new Intent(this, PickRegistration.class));
                break;
            case R.id.password_alert:
                startActivity(new Intent(this, ChangePasswordActivity.class));
                break;
            case android.R.id.home:
                finish();
                break;
        }
    }

    private int getDate(String cardId) {
        char[] charContent = cardId.toCharArray();
        int sum = (charContent[4] - '0') * 10;
        sum = sum + (charContent[5] - '0');
        return 2000 + sum + 4;
    }
}
